package org.team4.service.meta;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.dao.DAOFactory;
import org.team4.dao.meta.FieldDAO;
import org.team4.domain.VmetaField;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.Validate;


@Service
public class FieldService implements IService {

    private FieldDAO fieldDAO;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        fieldDAO = (FieldDAO) DAOFactory.getDAO(sessionFactory, FieldDAO.class);
    }

    /**
     * 获得字段
     * 
     * @param id
     * @return
     * @throws CRUDException
     */
    public VmetaField findFieldById(String id) throws CRUDException {
        return fieldDAO.get(id);
    }

    /**
     * 删除字段
     * 
     * @param idList
     * @throws CRUDException
     */
    public void deleteFields(List<String> idList) throws CRUDException {
        fieldDAO.deleteByIdList(idList);
    }

    /**
     * 根据模块查找字段
     * 
     * @param moduleId
     * @return
     * @throws CRUDException
     */
    public List<VmetaField> listFieldByModuleId(String moduleId) throws CRUDException {
        if (Validate.notNull(moduleId))
            return fieldDAO.find(SQLConstant.FIELD_BY_MODULE_HQL, moduleId);
        return null;
    }

}